package com.bflow.web.controller;

import com.bflow.api.IBflowDefineProcessDrawService;
import com.bflow.api.IBflowDefineProcessService;
import com.bflow.api.bo.BflowDefineActQueryParam;
import com.bflow.api.bo.BflowDefineProcessBo;
import com.bflow.api.bo.BflowDefineProcessDrawBo;
import com.bflow.api.bo.draw.DrawBo;
import com.bflow.api.bo.ext.BflowUserBo;
import com.bflow.api.constant.BflowConstant;
import com.bflow.commons.bean.PageResult;
import com.bflow.commons.consts.CommConst;
import com.bflow.util.JsonUtil;
import com.bflow.web.util.TestLoginUtil;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;

/**
 * 画板
 */
@Controller
@RequestMapping(value = "/bflow/draw")
public class BflowDrawController extends BflowBaseController {

    @Autowired
    private IBflowDefineProcessDrawService<?, BflowDefineProcessDrawBo> bflowDefineProcessDrawService;


    @Autowired
    private IBflowDefineProcessService<?, BflowDefineProcessBo> bflowDefineProcessService;

    @RequestMapping(value = "get")
    @ResponseBody
    public String get(@RequestParam("id") String procId, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            DrawBo drawBo = bflowDefineProcessDrawService.getDraw(procId);
            return returnOk(drawBo,"查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }



    @RequestMapping(value = "getByInstancesId")
    @ResponseBody
    public String getByInstancesId(@RequestParam("id") String instancesId, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            DrawBo drawBo = bflowDefineProcessDrawService.getDrawByInstanceId(instancesId,user.getUserId().toString());
            return returnOk(drawBo,"查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public String edit(BflowDefineProcessDrawBo bo, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            //String json  = StringEscapeUtils.unescapeHtml3(bo.getJson());
            //insert
            if(bo.getId()==null){

                BflowDefineProcessBo process = bflowDefineProcessService.findById(bo.getProcId());
                bo.setCreateTime(new Date());
                bo.setDelFlag(CommConst.STATUS_Y);
                // 实例id
                bo.setProcId(bo.getProcId());
                bo.setDrawKey(process.getKey());
                bflowDefineProcessDrawService.save(bo);
            }else{
            //update
                bo.setUpdateTime(new Date());
                bflowDefineProcessDrawService.update(bo);
            }
            return returnOk("保存成功");
        } catch (Exception e) {
            return returnFail(e, "保存失败");
        }
    }
}
